package com.miya.demo.handler.functional;

import cn.hutool.core.map.MapUtil;
import com.miya.demo.handler.GroupBuyOrderHandler;
import com.miya.demo.handler.SpikeOrderHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

@Component
public class StrategyDemo {
    @Autowired
    private GroupBuyOrderHandler groupBuyOrderHandler;
    @Autowired
    private SpikeOrderHandler spikeOrderHandler;

    /**
     * Function<T,R>: 接收参数，并返回结果，主要方法 R apply(T t)
     * Consumer<T>: 接收参数，无返回，主要方法 accept(T t)
     * Supplier<R>: 无接收参数，但返回结果，主要方法 T get()
     * Predicate<T>: 接收参数，并返回boolean，主要方法 boolean test(T t)
     */
    private Map<Integer, Consumer<Map<String, Object>>> orderStrategyMap = MapUtil.newHashMap();

    @PostConstruct
    public void initOrderStrategy() {
        orderStrategyMap.put(2, params -> groupBuyOrderHandler.handle(params));
        orderStrategyMap.put(1, params -> spikeOrderHandler.handle(params));
    }

    public void execute(Integer orderType) {
        orderStrategyMap.get(orderType).accept(new HashMap<>(8));
    }

}
